using System;
using System.Linq;
using System.Text;
using DevExpress.Xpo;
using DevExpress.ExpressApp;
using System.ComponentModel;
using DevExpress.ExpressApp.DC;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.Base;
using System.Collections.Generic;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;

namespace DomainComponents.Common
{

    public enum Gender
    {
        None = 0,
        Male = 1,
        Female = 2
    }

    public enum MartialStatus
    {
        None = 0,
        Single = 1,
        Married = 2,
        Divorced = 3,
        Widowed = 4
    }

    public class PersonValidationRules
    {
        public const string LastNameIsRequired = "PersonLastNameIsRequired";
    }

    [DomainComponent, XafDefaultProperty("FullName")]
    public interface IPerson
    {
        [Calculated(PersonLogic.FullNameExpressionDefault)]
        string FullName { get; }
        [RuleRequiredField(PersonValidationRules.LastNameIsRequired, DefaultContexts.Save)]
        string LastName { get; set; }
        string FirstName { get; set; }
        DateTime Birthday { get; set; }
        string JobTitle { get; set; }
        Gender Gender { get; set; }
        MartialStatus MartialStatus { get; set; }
        string SpouseName { get; set; }
        DateTime Anniversary { get; set; }
    }

    [DomainLogic(typeof(IPerson))]
    public class PersonLogic
    {
        public static void SetFullNameExpression(string expression)
        {
            ITypeInfo personTypeInfo = XafTypesInfo.Instance.FindTypeInfo(typeof(IPerson));
            IMemberInfo memberInfo = personTypeInfo.FindMember("FullName");
            CalculatedAttribute attribute = memberInfo.FindAttribute<CalculatedAttribute>();
            if (attribute == null)
            {
                memberInfo.AddAttribute(new CalculatedAttribute(expression));
            }
            else
            {
                if (attribute.Expression != expression)
                {
                    ((BaseInfo)memberInfo).RemoveAttribute(attribute);
                    memberInfo.AddAttribute(new CalculatedAttribute(expression));
                }
            }
        }
        public const string FullNameExpressionDefault = "concat(isnull(FirstName, ''), ' ', isnull(LastName, ''))";
        //public const string FullNameExpressionDefault = "concat(isnull(FirstName, ''), iif(IsNullOrEmpty(FirstName) or IsNullOrEmpty(LastName), '', ' '),  isnull(LastName, ''))";
    }

    //[DefaultClassOptions]
    ////[ImageName("BO_Contact")]
    ////[DefaultProperty("DisplayMemberNameForLookupEditorsOfThisType")]
    ////[DefaultListViewOptions(MasterDetailMode.ListViewOnly, false, NewItemRowPosition.None)]
    ////[Persistent("DatabaseTableName")]
    //// Specify more UI options using a declarative approach (http://documentation.devexpress.com/#Xaf/CustomDocument2701).
    //public class IPerson : BaseObject
    //{ // Inherit from a different class to provide a custom primary key, concurrency and deletion behavior, etc. (http://documentation.devexpress.com/#Xaf/CustomDocument3146).
    //    public IPerson(Session session)
    //        : base(session)
    //    {
    //    }
    //    public override void AfterConstruction()
    //    {
    //        base.AfterConstruction();
    //        // Place your initialization code here (http://documentation.devexpress.com/#Xaf/CustomDocument2834).
    //    }
    //    //private string _PersistentProperty;
    //    //[XafDisplayName("My display name"), ToolTip("My hint message")]
    //    //[ModelDefault("EditMask", "(000)-00"), Index(0), VisibleInListView(false)]
    //    //[Persistent("DatabaseColumnName"), RuleRequiredField(DefaultContexts.Save)]
    //    //public string PersistentProperty {
    //    //    get { return _PersistentProperty; }
    //    //    set { SetPropertyValue("PersistentProperty", ref _PersistentProperty, value); }
    //    //}

    //    //[Action(Caption = "My UI Action", ConfirmationMessage = "Are you sure?", ImageName = "Attention", AutoCommit = true)]
    //    //public void ActionMethod() {
    //    //    // Trigger a custom business logic for the current record in the UI (http://documentation.devexpress.com/#Xaf/CustomDocument2619).
    //    //    this.PersistentProperty = "Paid";
    //    //}
    //}
}
